package io.github.wppli.domain.user.service;

import cc.jq1024.middleware.redisson.IRedissonClientService;
import io.github.wppli.domain.user.model.entity.FollowUserEntity;
import io.github.wppli.domain.user.model.valobj.FollowConstants;
import io.github.wppli.domain.user.repository.IAdjustAccountRepository;
import io.github.wppli.domain.user.repository.IUserRepository;
import org.redisson.api.RSet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author li--jiaqiang 2025−03−03
 */
@Service
public class AdjustAccountService {

    private static final Logger log = LoggerFactory.getLogger(AdjustAccountService.class);
    private final IAdjustAccountRepository adjustAccountRepository;
    private final IRedissonClientService redissonClientService;
    private final IUserRepository userRepository;

    public AdjustAccountService(IAdjustAccountRepository adjustAccountRepository, IRedissonClientService redissonClientService, IUserRepository userRepository) {
        this.adjustAccountRepository = adjustAccountRepository;
        this.redissonClientService = redissonClientService;
        this.userRepository = userRepository;
    }

    public void adjustFollowCount(Long userId, Long targetUserId, boolean followed) {
        adjustAccountRepository.adjustFollowCount(userId, targetUserId, followed);
    }

    /**
     * 调整关注数
     * @param userId 用户ID
     * @param targetUser 被关注用户
     */
    public void increaseFolloweeCountRedis(Long userId, FollowUserEntity targetUser) {
        Long target = targetUser.getId();
        log.info("将用户：{} 添加到用户：{} 的关注列表中", userId, target);
        String key = FollowConstants.FOLLOWEE.getPrefix() + userId;
        RSet<Object> userIdFollows = redissonClientService.getSet(key);
        userIdFollows.add(targetUser);
    }
    public void decreaseFolloweeCountRedis(Long userId, FollowUserEntity targetUser) {
        Long target = targetUser.getId();
        log.info("将用户：{} 从用户：{} 的关注列表中移除", userId, target);
        String key = FollowConstants.FOLLOWEE.getPrefix() + userId;
        RSet<Object> userIdFollows = redissonClientService.getSet(key);
        userIdFollows.remove(targetUser);
    }
    public void adjustFolloweeCountRedis(Long userId, Long targetUserId, boolean followed) {
        FollowUserEntity targetUser = userRepository.queryFollowUserInfoById(targetUserId);
        if (followed) {
            increaseFolloweeCountRedis(userId, targetUser);
        } else {
            decreaseFolloweeCountRedis(userId, targetUser);
        }
    }

    /**
     * 调整粉丝数
     */
    public void increaseFollowerCountRedis(FollowUserEntity targetUser, Long userId) {
        Long target = targetUser.getId();
        log.info("将用户：{} 添加到用户：{} 的粉丝列表中", userId, target);
        String key = FollowConstants.FOLLOWER.getPrefix() + target;
        RSet<Object> userIdFollows = redissonClientService.getSet(key);
        userIdFollows.add(userId);
    }
    public void decreaseFollowerCountRedis(FollowUserEntity targetUser, Long userId) {
        Long target = targetUser.getId();

        log.info("将用户：{} 从用户：{} 的粉丝列表中移除", userId, target);
        String key = FollowConstants.FOLLOWER.getPrefix() + target;
        RSet<Object> userIdFollows = redissonClientService.getSet(key);
        userIdFollows.remove(userId);
    }
    public void adjustFollowerCountRedis(Long userId, Long targetUserId, boolean followed) {
        FollowUserEntity userEntity = userRepository.queryFollowUserInfoById(userId);
        if (followed) {
            increaseFollowerCountRedis(userEntity, targetUserId);
        } else {
            decreaseFollowerCountRedis(userEntity, targetUserId);
        }
    }

    /**
     * 获取关注列表
     * @param userId 用户
     */
    public List<FollowUserEntity> getFolloweeList(Long userId) {
        return adjustAccountRepository.getFolloweeList(userId);
    }
}
